﻿using Iworld.basis.Service;
using Iworld.Basis.Manager;
using Iworld.Game.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Backstage.Import
{
    /// <summary>
    /// 编辑牌局的数据集
    /// </summary>
    [DataContract]
    public class EditGameRoundImport : CheckAdministratorImportBase, IPackageForUpdateModel<IModelToDbContextOfGame,GameChapter>
    {
        [DataMember]
        public int RoundId { set; get; }

        /// <summary>
        /// 改变后的数字
        /// </summary>
        [DataMember]
        public PokerNumber PokerNumber { set; get; }

        /// <summary>
         /// 改变后的花色
        /// </summary>
         [DataMember]
        public PokerColor PokerColor { set; get; }

         public int Id
         {
             get { return ChapterId; }
         }

         public override void CheckAllowExecuteOrNot(Main.Impl.ModelToDbContext db)
         {
             base.CheckAllowExecuteOrNot(db);
             if (!Self.Group.CanEditGameRound)
             {
                 throw new Exception("你无权编辑牌局");
             }

         }

         public int ChapterId { set; get; }

         public void CheckData(IModelToDbContextOfGame db)
         {
             GameRound r = db.GameRounds.Find(this.RoundId);
             if(r == null)
             {
                 throw new Exception("牌局不存在");
             }
             if (r.IsLottery || r.GameChapter.Status != GameChapterStatus.从未开奖)
             {
                 throw new Exception("牌轮已经开始,不能修改");
             }
             this.ChapterId = r.GameChapter.Id;
         }

         public void Update(GameChapter model)
         {
            GameRound gr= model.GameRounds.Where(x=>x.Id == RoundId).Single();
            gr.PokerColor = this.PokerColor;
            gr.PokerNumber = this.PokerNumber;
            gr.IsEdited = true;
            gr.EditTime = DateTime.Now;
            gr.EditAdministratorId = this.Self.Id;

         }
    }
}
